package com.demo2do.boyle.client.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.service.WalletService;
import com.demo2do.boyle.common.entity.customer.wallet.Wallet;
import com.demo2do.boyle.common.entity.customer.wallet.WalletRecord;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author Wilson
 */
@Service("walletService")
public class WalletServiceImpl extends GenericServiceImpl<Wallet> implements WalletService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.WalletService#load(com.demo2do.boyle.common.entity.customer.Customer)
     */
    public Wallet load(Integer customerId) {

        Wallet wallet = genericDaoSupport.load(Wallet.class, customerId);

        return (wallet == null ? null : wallet);
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.WalletService#listRecords(com.demo2do.boyle.common.entity.customer.wallet.Wallet)
     */
    @SuppressWarnings("unchecked")
    public List<WalletRecord> listRecords(Wallet wallet, Page page) {
    	List<WalletRecord> walletRecords =  genericDaoSupport.searchForList("FROM WalletRecord WHERE disabled = false AND wallet = :wallet", "wallet", wallet ,page.getBeginIndex(), page.getMaxResultRecords());
    	return walletRecords.size() > page.getEveryPage() ? walletRecords.subList(0, page.getEveryPage()) : walletRecords;

    }

	@Override
	public void updateWalletFrozenAmount(Wallet wallet,Integer money) {
		// update wallet balance and cashOut 
		genericDaoSupport.update(wallet.addFrozenAmount(money));
	}

}
